﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;


namespace Nevernote_client
{
    public partial class Form1 : Form
    {
       // string str_note = null;
        public Form1()
        {
            InitializeComponent();
            TreeNode node = new TreeNode("Nevernote");
            dic_treeView.Nodes.Add(node);
            GetTreeViewData("D:\\Nevernote", node);
            filename_textBox.Enabled = false;
            note_richTextBox.Enabled = false;
            edit_toolStrip.Enabled = false;
            vieworder_comboBox.Items.Add("最近修改");
            vieworder_comboBox.Items.Add("最近创建");
            vieworder_comboBox.Items.Add("标题升序");
            vieworder_comboBox.Items.Add("标题降序");
            this.BackgroundImage = Image.FromFile("海底世界.png");
            海底世界ToolStripMenuItem.Checked = true;
        }
        TreeNode current_node = null;
        TreeNode current_folder = null;
        string sourcepath = "";//当前文件未改名的源路径
       
        //初始化添加树节点
        private void GetTreeViewData(string path, TreeNode node)
        {
            //获取指定路径下的所有文件夹集合,GetFiles()则会包含文件,如:a.jpg。
            string[] dirs = Directory.GetDirectories(path);
            string[] file = Directory.GetFiles(path);
            string dirPath = "";
            string filePath = "";
            for (int i = 0; i < dirs.Length; i++)
            {

                //得到指定文件夹的子级文件夹,子级文件夹通过自调自获取其子级文件夹,

               //这样一层一层得到指定文件下的所有文件夹。
                string childpath = dirs[i];
                //截取字符串,把路径去掉,只显示文件夹名称。
                dirPath = childpath.Substring(childpath.LastIndexOf("\\") + 1,childpath.Length -childpath.LastIndexOf("\\")-1);
                TreeNode child = new TreeNode(dirPath,0,0); // 把文件夹增加到TreeNode。
                child.Name = childpath;
                node.Nodes.Add(child);
                GetTreeViewData(childpath, child);  //递归调用.
            }
            for (int j = 0; j < file.Length; j++)
            {

                //得到指定文件夹的子级文件夹,子级文件夹通过自调自获取其子级文件夹,

                //这样一层一层得到指定文件下的所有文件夹。
                string childpath = file[j];
                //截取字符串,把路径去掉,只显示文件夹名称。
                filePath = childpath.Substring(childpath.LastIndexOf("\\") + 1, childpath.Length - childpath.LastIndexOf("\\") - 1);
                TreeNode child = new TreeNode(filePath, 0, 0); // 把文件增加到TreeNode。
                child.Name = childpath;
                node.Nodes.Add(child);
            }
        }

        //树中选中某个文件或文件夹

        private void dic_treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            current_node = e.Node;
            string nodepath = getnodepath(e.Node);
            sourcepath = nodepath;
            string filename = "";
            string filecontent = "";
            string show = "";
            string createdtime = "";
            string modifedtime = "";
            if (e.Button ==MouseButtons.Left)
            {
                if (nodepath.EndsWith(".rtf"))
                {
                    edit_button.Enabled = true;
                    delete_button.Enabled = true;
                    current_folder = current_node.Parent;
                    filename = current_node.Text.Substring(0, current_node.Text.Length - 4);
                    filename_textBox.Text = filename;
                    note_richTextBox.LoadFile(nodepath);
                    filecontent = note_richTextBox.Text;
                    FileInfo fi = new FileInfo(nodepath);
                    createdtime = createdtime_label.Text = fi.CreationTime.ToString();
                    modifedtime = modifedtime_label.Text = fi.LastWriteTime.ToString();
                    file_listBox.SelectedItem = filename + "  " + "创建时间:" + createdtime + "  " + "修改时间:" + modifedtime + "\n" + filecontent;
                }
                else
                {
                    filename_textBox.Enabled = false;
                    note_richTextBox.Enabled = false;
                    edit_toolStrip.Enabled = false;
                    current_folder = current_node;
                    file_listBox.Items.Clear();
                    for (int i = 0; i < current_node.Nodes.Count; i++)
                    {
                        if (current_node.Nodes[i].Text.EndsWith(".rtf"))
                        {
                            filename = current_node.Nodes[i].Text.Substring(0, current_node.Nodes[i].Text.Length - 4);
                            nodepath = getnodepath(current_node.Nodes[i]);
                            RichTextBox temp_rtb = new RichTextBox();
                            temp_rtb.LoadFile(nodepath);
                            filecontent = temp_rtb.Text;
                            FileInfo fi = new FileInfo(nodepath);
                            createdtime = fi.CreationTime.ToString();
                            modifedtime = fi.LastWriteTime.ToString();
                            show = filename + "  " + "创建时间:" + createdtime + "  " + "修改时间:" + modifedtime + "\n" + filecontent;
                            file_listBox.Items.Add(show);


                        }

                    }
                }
            }
           
               
        }

        //获取节点路径
        private string getnodepath(TreeNode node)
        {
            string nodepath = node.Text;
            while (node.Parent != null)
            {
                nodepath = node.Parent.Text + "\\" + nodepath;
                node = node.Parent;
            }
            nodepath = "D:\\" + nodepath;
            return nodepath;
        }


        //打开节点菜单
        private void dic_treeView_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            { 
                TreeNode tn=dic_treeView.GetNodeAt(e.X,e.Y);
                if (tn != null) 
                {
                 dic_treeView.SelectedNode = tn;current_node=tn;
                 dic_treeView.ContextMenuStrip = treeview_contextMenuStrip;
                 if (current_node.Text.EndsWith(".rtf"))
                 {
                     newfile_ToolStripMenuItem.Enabled = false;
                     newfolder_ToolStripMenuItem.Enabled = false;
                 }
                 else 
                 {
                     newfile_ToolStripMenuItem.Enabled = true;
                     newfolder_ToolStripMenuItem.Enabled = true;
                 }
                } 
                   
            }
                

        }

        
        //新建文件夹
        private void newfolder_ToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
        {
            for (int i = 0; i < current_node.Nodes.Count; i++)
            {
                if (current_node.Nodes[i].Text == "新建文件夹")
                {
                    MessageBox.Show("已有新建文件夹,请修改原文件夹名!");
                    return;
                }
            }
            string nodepath = getnodepath(current_node)+"\\新建文件夹";
            TreeNode newnode=new TreeNode("新建文件夹");
            current_node.Nodes.Add(newnode);
            System.IO.Directory.CreateDirectory(nodepath);
        }
        //新建文件
        private void newfile_ToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
        {
            for (int i = 0; i < current_node.Nodes.Count; i++)
            {
                if (current_node.Nodes[i].Text == "新建文件.rtf")
                {
                    MessageBox.Show("已有新建文件,请修改原文件名!");
                    return;
                }
            }
            string nodepath = getnodepath(current_node) + "\\新建文件.rtf";
            TreeNode newnode = new TreeNode("新建文件.rtf");
            current_node.Nodes.Add(newnode);
            current_node = newnode;
            dic_treeView.SelectedNode = newnode;
            System.IO.File.CreateText(nodepath);
            filename_textBox.Enabled = true;
            note_richTextBox.Enabled = true;
            edit_toolStrip.Enabled = true;
            note_richTextBox.Clear();
            createdtime_label.Text = "";
            modifedtime_label.Text = "";
            filename_textBox.Text = "新建文件";
           // System.IO.
        }
      
        //重新命名
        private void rename_ToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
        {
            string newname = "";
            Rename rename = new Rename();
            rename.file_or_dir(current_node);
            rename.ShowDialog(this);   
            newname = rename.newname;
            string newpath = "";
            if (current_node.Text.EndsWith(".rtf"))
            {
                if (newname != "")
                {
                    filename_textBox.Text = newname;
                    current_node.Text = newname + ".rtf";
                newpath = getnodepath(current_node);
                System.IO.File.Move(sourcepath, newpath);
                }
               
            }
            else 
            {
                if (newname != "")
                {
                    current_node.Text = newname;
                newpath = getnodepath(current_node);
                System.IO.Directory.Move(sourcepath, newpath);
                }
            }
            

        }
        //保存文件
        private void button_save_Click(object sender, EventArgs e)
        {

            if (note_richTextBox.Text == "")
                MessageBox.Show("保存文件内容不能为空!");
            else 
            {
            if (current_node.Text != (filename_textBox.Text + ".rtf"))
            {
                current_node.Text = filename_textBox.Text + ".rtf";
                string newfilepath = getnodepath(current_node);
                System.IO.File.Move(sourcepath, newfilepath);
                FileInfo fi = new FileInfo(newfilepath);
                createdtime_label.Text = fi.CreationTime.ToString();
                modifedtime_label.Text = fi.LastWriteTime.ToString();
            }
            else
            {
               string newfilepath = getnodepath(current_node);
               this.note_richTextBox.SaveFile(@newfilepath);
               FileInfo fi = new FileInfo(newfilepath);
               createdtime_label.Text = fi.CreationTime.ToString();
               modifedtime_label.Text = fi.LastWriteTime.ToString();
            }
            }
           
           

        }
        //编辑
        private void edit_button_Click(object sender, EventArgs e)
        {
            filename_textBox.Enabled = true;
            note_richTextBox.Enabled = true;
            edit_toolStrip.Enabled = true;
            edit_init();
            note_richTextBox.Focus();
            edit_button.Enabled = false;

        }
        //删除
        private void delete_button_Click(object sender, EventArgs e)
        {
            string filename = filename_textBox.Text;
            string filepath = getnodepath(current_node);
            dic_treeView.Nodes.Remove(current_node);
            System.IO.File.Delete(filepath);
            filename_textBox.Clear();
            note_richTextBox.Clear();
        }
        //加粗斜体下划线
        private void ChangeFontStyle(FontStyle style)    //加粗，加斜，下划线通用方法
        {
            RichTextBox tempRichTextBox = new RichTextBox();  //将要存放被选中文本的副本   
            int curRtbStart = note_richTextBox.SelectionStart;
            int len = note_richTextBox.SelectionLength;
            int tempRtbStart = 0;
            Font font = note_richTextBox.SelectionFont;
            if (len <= 1 && font != null) 
            {
                if (style == FontStyle.Bold && font.Bold ||
                    style == FontStyle.Italic && font.Italic ||
                    style == FontStyle.Underline && font.Underline)
                {
                    note_richTextBox.SelectionFont = new Font(font, font.Style ^ style);
                }
                else if (style == FontStyle.Bold && !font.Bold ||
                         style == FontStyle.Italic && !font.Italic ||
                         style == FontStyle.Underline && !font.Underline)
                {
                    note_richTextBox.SelectionFont = new Font(font, font.Style | style);
                }
                return;
            }
            tempRichTextBox.Rtf = note_richTextBox.SelectedRtf;
            tempRichTextBox.Select(len - 1, 1); //选中副本中的最后一个文字   
            //克隆被选中的文字Font，这个tempFont主要是用来判断   
            //最终被选中的文字是否要加粗、去粗、斜体、去斜、下划线、去下划线   
            Font tempFont = (Font)tempRichTextBox.SelectionFont.Clone();

            //清空2和3   
            for (int i = 0; i < len; i++)
            {
                tempRichTextBox.Select(tempRtbStart + i, 1);  //每次选中一个，逐个进行加粗或去粗   
                if (style == FontStyle.Bold && tempFont.Bold ||
                    style == FontStyle.Italic && tempFont.Italic ||
                    style == FontStyle.Underline && tempFont.Underline)
                {
                    tempRichTextBox.SelectionFont =
                        new Font(tempRichTextBox.SelectionFont,
                                 tempRichTextBox.SelectionFont.Style ^ style);
                }
                else if (style == FontStyle.Bold && !tempFont.Bold ||
                         style == FontStyle.Italic && !tempFont.Italic ||
                         style == FontStyle.Underline && !tempFont.Underline)
                {
                    tempRichTextBox.SelectionFont =
                        new Font(tempRichTextBox.SelectionFont,
                                 tempRichTextBox.SelectionFont.Style | style);
                }
            }
            tempRichTextBox.Select(tempRtbStart, len);
            note_richTextBox.SelectedRtf = tempRichTextBox.SelectedRtf; //将设置格式后的副本拷贝给原型   
            note_richTextBox.Select(curRtbStart, len);
            refreshtoolbar();
        }
        //加粗
        private void bold_toolStripButton_Click(object sender, EventArgs e)
        {
            FontStyle style = FontStyle.Bold;
            ChangeFontStyle(style);
        }
        //斜体
        private void italic_toolStripButton_Click(object sender, EventArgs e)
        {
            FontStyle style = FontStyle.Italic;
            ChangeFontStyle(style);
        }
        //下划线
        private void underline_toolStripButton_Click(object sender, EventArgs e)
        {
            FontStyle style = FontStyle.Underline;
            ChangeFontStyle(style);
        }

        //选中改变
        private void note_richTextBox_SelectionChanged(object sender, EventArgs e)
        {
            refreshtoolbar();

        }
        public void refreshtoolbar()
        {
            Font font = note_richTextBox.SelectionFont;
            if (font != null)
            {
                if (FontStyle.Bold == FontStyle.Bold && font.Bold)
                {
                    bold_toolStripButton.BackColor = Color.Blue;
                }
                else bold_toolStripButton.BackColor = Color.Empty;

                if (FontStyle.Italic == FontStyle.Italic && font.Italic)
                {
                    italic_toolStripButton.BackColor = Color.Blue;
                }
                else italic_toolStripButton.BackColor = Color.Empty;

                if (FontStyle.Underline == FontStyle.Underline && font.Underline)
                {
                    underline_toolStripButton.BackColor = Color.Blue;
                }
                else underline_toolStripButton.BackColor = Color.Empty;

                if (note_richTextBox.SelectionCharOffset == 5)
                    up_toolStripButton.BackColor = Color.Blue;
                else
                    up_toolStripButton.BackColor = Color.Empty;

                if (note_richTextBox.SelectionCharOffset == -5)
                    down_toolStripButton.BackColor = Color.Blue;
                else
                    down_toolStripButton.BackColor = Color.Empty;

                float fontsize = font.Size;
                size_toolStripComboBox.Text = fontsize.ToString();
                
                fontname_toolStripComboBox.Text = font.Name;                
            }
            else
            {
                bold_toolStripButton.BackColor = Color.Empty;
                italic_toolStripButton.BackColor = Color.Empty;
                underline_toolStripButton.BackColor = Color.Empty;
                fontname_toolStripComboBox.Text = "";
            }
        }
        //改变字号
        private void size_toolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ////获取字号
            string ssize = "";
            ssize = size_toolStripComboBox.SelectedItem.ToString();
            float fontSize = float.Parse(ssize);
            //改变字号大小
            changeselectedfontsize(fontSize); 
        }
        public void changeselectedfontsize(float size)
        {
            float fontSize = size;
            RichTextBox tempRichTextBox = new RichTextBox();

            int curRtbStart = note_richTextBox.SelectionStart;
            int len = note_richTextBox.SelectionLength;
            int tempRtbStart = 0;

            Font font = note_richTextBox.SelectionFont;
            if (len <= 1 && null != font)
            {
                note_richTextBox.SelectionFont = new Font(font.Name, fontSize, font.Style);
                return;
            }
            tempRichTextBox.Rtf = note_richTextBox.SelectedRtf;
            for (int i = 0; i < len; i++)
            {
                tempRichTextBox.Select(tempRtbStart + i, 1);
                tempRichTextBox.SelectionFont =
                    new Font(tempRichTextBox.SelectionFont.Name,
                             fontSize, tempRichTextBox.SelectionFont.Style);
            }
            tempRichTextBox.Select(tempRtbStart, len);
            note_richTextBox.SelectedRtf = tempRichTextBox.SelectedRtf;
            note_richTextBox.Select(curRtbStart, len);
            note_richTextBox.Focus();   
        }
        //private void size_toolStripComboBox_Click(object sender, EventArgs e)
        //{
        //    MessageBox.Show("hello");
        //}
        private void edit_init()  //初始化工具栏
        {
            size_toolStripComboBox.Items.Add("5");
            size_toolStripComboBox.Items.Add("5.5");
            size_toolStripComboBox.Items.Add("6.5");
            size_toolStripComboBox.Items.Add("7");
            size_toolStripComboBox.Items.Add("8");
            size_toolStripComboBox.Items.Add("9");
            size_toolStripComboBox.Items.Add("10");
            size_toolStripComboBox.Items.Add("10.5");
            size_toolStripComboBox.Items.Add("11");
            size_toolStripComboBox.Items.Add("12");
            size_toolStripComboBox.Items.Add("14");
            size_toolStripComboBox.Items.Add("16");
            size_toolStripComboBox.Items.Add("18");
            size_toolStripComboBox.Items.Add("20");
            size_toolStripComboBox.Items.Add("22");
            size_toolStripComboBox.Items.Add("24");
            size_toolStripComboBox.Items.Add("26");
            size_toolStripComboBox.Items.Add("28");
            size_toolStripComboBox.SelectedIndex = 8;
            //Font font = note_richTextBox.SelectionFont;
            //note_richTextBox.SelectionFont = new Font(font.Name, 24, font.Style);

            //读取系统字体#89
            System.Drawing.Text.InstalledFontCollection myFonts = new System.Drawing.Text.InstalledFontCollection();
            foreach(FontFamily currFont in myFonts.Families)
            {
                fontname_toolStripComboBox.Items.Add(currFont.Name);
            }
            fontname_toolStripComboBox.SelectedIndex = 257;//宋体


            return;
        }
        //左右对齐,居中
        private void left_toolStripButton7_Click(object sender, EventArgs e)
        {
                if (note_richTextBox.SelectionAlignment != HorizontalAlignment.Left)
                    note_richTextBox.SelectionAlignment = HorizontalAlignment.Left;
        }

        private void minddle_toolStripButton8_Click(object sender, EventArgs e)
        {
                if (note_richTextBox.SelectionAlignment != HorizontalAlignment.Center)
                    note_richTextBox.SelectionAlignment = HorizontalAlignment.Center;
        }

        private void right_toolStripButton9_Click(object sender, EventArgs e)
        {
                if (note_richTextBox.SelectionAlignment != HorizontalAlignment.Right)
                    note_richTextBox.SelectionAlignment = HorizontalAlignment.Right;
        }

        private void up_toolStripButton_Click(object sender, EventArgs e)
        {
            if (note_richTextBox.SelectionCharOffset != 5)
            {
                //float cursize = note_richTextBox.SelectionFont.Size;
                //changeselectedfontsize(cursize / 2);
                note_richTextBox.SelectionCharOffset = 5;
                //up_toolStripButton.BackColor = Color.Blue;
            }
            else
            {
                //float cursize = note_richTextBox.SelectionFont.Size;
                //changeselectedfontsize(cursize * 2);
                note_richTextBox.SelectionCharOffset = 0;
                //up_toolStripButton.BackColor = Color.Empty;
            }
            refreshtoolbar();
        }

        private void down_toolStripButton_Click(object sender, EventArgs e)
        {
            if (note_richTextBox.SelectionCharOffset != -5)
            {
                note_richTextBox.SelectionCharOffset = -5;
                //down_toolStripButton.BackColor = Color.Blue;
            }
            else
            {
                note_richTextBox.SelectionCharOffset = 0;
                //down_toolStripButton.BackColor = Color.Empty;
            }
            refreshtoolbar();
        }
        //改变字体
        private void fontname_toolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (note_richTextBox.SelectionLength > 0)
            {
                float fontSize = 0;
                if (note_richTextBox.SelectionFont != null)
                {
                    fontSize = note_richTextBox.SelectionFont.Size;
                }
                else
                {
                    string ssize = "";
                    ssize = size_toolStripComboBox.SelectedItem.ToString();
                    fontSize = float.Parse(ssize);
                }
                FontConverter fc = new FontConverter();
                string fontName = fontname_toolStripComboBox.SelectedItem.ToString();
                try
                {
                    Font selectedFont = (Font)fc.ConvertFromString(fontName);
                    Font newFont = new Font(selectedFont.Name, fontSize, selectedFont.Style);
                    note_richTextBox.SelectionFont = newFont;
                }
                catch
                {
                }
                
            }
            else
            {
                ////获取字号
                string ssize = "";
                ssize = size_toolStripComboBox.SelectedItem.ToString();
                float fontSize = float.Parse(ssize);
                ////
                FontConverter fc = new FontConverter();
                string fontName = fontname_toolStripComboBox.SelectedItem.ToString();
                try
                {
                    Font selectedFont = (Font)fc.ConvertFromString(fontName);
                    Font newFont = new Font(selectedFont.Name, fontSize, selectedFont.Style);
                    note_richTextBox.SelectionFont = newFont;
                    ////////////////////////////////////////////////////////////////////fuck
                }
                catch
                {
                }
            }

        }
        //listbox添加项
        private void file_listBox_DrawItem_1(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            Brush myBrush = Brushes.Black;
            switch (e.Index % 2)
            {
                case 0:
                    myBrush = Brushes.Black;
                    break;
                case 1:
                    myBrush = Brushes.Blue;
                    break;
            }
            e.Graphics.DrawString(file_listBox.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds);
            
        }
        //改变文件排序
        private void vieworder_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            FileInfor[] arr_filelist = new FileInfor[current_folder.Nodes.Count];
            int min = 0;
            string show = "";
            string filecontent = "";
            file_listBox.Items.Clear();
            for (int i = 0; i < current_folder.Nodes.Count; i++)
            {
                string nodepath=getnodepath(current_folder.Nodes[i]);
                FileInfo fi = new FileInfo(nodepath);
                arr_filelist[i] = new FileInfor(current_folder.Nodes[i].Text.Substring(0,current_folder.Nodes[i].Text.Length-4), fi.CreationTime.ToString(), fi.LastWriteTime.ToString(), nodepath);
            }
                switch (vieworder_comboBox.SelectedIndex)
                {
                    case 0:
                        for (int i = 0; i < arr_filelist.Length - 1; ++i)
                        {
                            min = i;
                            for (int j = i + 1; j < arr_filelist.Length; ++j)
                            {
                                if (arr_filelist[j].Modifetime.CompareTo(arr_filelist[min].Modifetime) <0 )
                                    min = j;
                            }
                            FileInfor t = arr_filelist[min];
                            arr_filelist[min] = arr_filelist[i];
                            arr_filelist[i] = t;
                        }
                        for (int k = 0; k < arr_filelist.Length; k++)
                        {
                            RichTextBox temp_rtb = new RichTextBox();
                            temp_rtb.LoadFile(arr_filelist[k].Filepath);
                            filecontent = temp_rtb.Text;
                            show = arr_filelist[k].Filename + "  " + "创建时间:" + arr_filelist[k].Createtime + "  " + "修改时间:" + arr_filelist[k].Modifetime + "\n" + filecontent;
                            file_listBox.Items.Add(show);
                        }
                            
                        break;
                    case 1:
                        for (int i = 0; i < arr_filelist.Length - 1; ++i)
                        {
                            min = i;
                            for (int j = i + 1; j < arr_filelist.Length; ++j)
                            {
                                if (arr_filelist[j].Createtime.CompareTo(arr_filelist[min].Createtime) < 0)
                                    min = j;
                            }
                            FileInfor t = arr_filelist[min];
                            arr_filelist[min] = arr_filelist[i];
                            arr_filelist[i] = t;
                        }
                        for (int k = 0; k < arr_filelist.Length; k++)
                        {
                            RichTextBox temp_rtb = new RichTextBox();
                            temp_rtb.LoadFile(arr_filelist[k].Filepath);
                            filecontent = temp_rtb.Text;
                            show = arr_filelist[k].Filename + "  " + "创建时间:" + arr_filelist[k].Createtime + "  " + "修改时间:" + arr_filelist[k].Modifetime + "\n" + filecontent;
                            file_listBox.Items.Add(show);
                        }
                        break;
                    case 2:
                        for (int i = 0; i < arr_filelist.Length - 1; ++i)
                        {
                            min = i;
                            for (int j = i + 1; j < arr_filelist.Length; ++j)
                            {
                                if (arr_filelist[j].Filename.CompareTo(arr_filelist[min].Filename) < 0)
                                    min = j;
                            }
                            FileInfor t = arr_filelist[min];
                            arr_filelist[min] = arr_filelist[i];
                            arr_filelist[i] = t;
                        }
                        for (int k = 0; k < arr_filelist.Length; k++)
                        {
                            RichTextBox temp_rtb = new RichTextBox();
                            temp_rtb.LoadFile(arr_filelist[k].Filepath);
                            filecontent = temp_rtb.Text;
                            show = arr_filelist[k].Filename + "  " + "创建时间:" + arr_filelist[k].Createtime + "  " + "修改时间:" + arr_filelist[k].Modifetime + "\n" + filecontent;
                            file_listBox.Items.Add(show);
                        }
                        break;
                    case 3:
                        for (int i = 0; i < arr_filelist.Length - 1; ++i)
                        {
                            min = i;
                            for (int j = i + 1; j < arr_filelist.Length; ++j)
                            {
                                if (arr_filelist[j].Filename.CompareTo(arr_filelist[min].Filename) > 0)
                                    min = j;
                            }
                            FileInfor t = arr_filelist[min];
                            arr_filelist[min] = arr_filelist[i];
                            arr_filelist[i] = t;
                        }
                        for (int k = 0; k < arr_filelist.Length; k++)
                        {
                            RichTextBox temp_rtb = new RichTextBox();
                            temp_rtb.LoadFile(arr_filelist[k].Filepath);
                            filecontent = temp_rtb.Text;
                            show = arr_filelist[k].Filename + "  " + "创建时间:" + arr_filelist[k].Createtime + "  " + "修改时间:" + arr_filelist[k].Modifetime + "\n" + filecontent;
                            file_listBox.Items.Add(show);
                        }
                        break;
                }
             
        }
        //file_listBox选中某个文件
        private void file_listBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string filename = file_listBox.SelectedItem.ToString().Substring(0, file_listBox.SelectedItem.ToString().IndexOf(" "));
            if (current_node.Text.EndsWith(".rtf"))
            {
                edit_button.Enabled = true;               
                TreeNode tempnode = current_node.Parent;
                for (int i = 0; i < tempnode.Nodes.Count; i++)
                {
                    if (tempnode.Nodes[i].Text == filename + ".rtf")
                    {
                        dic_treeView.SelectedNode = tempnode.Nodes[i];
                        dic_treeView.Focus();
                        string filepath = getnodepath(tempnode.Nodes[i]);
                        filename_textBox.Text = filename;
                        note_richTextBox.LoadFile(filepath);
                        FileInfo fi = new FileInfo(filepath);
                        createdtime_label.Text = fi.CreationTime.ToString();
                        modifedtime_label.Text = fi.LastWriteTime.ToString();
                        current_node = tempnode.Nodes[i];
                    }
                }

            }
            else
            {
                for (int i = 0; i < current_node.Nodes.Count; i++)
                {
                    if (current_node.Nodes[i].Text == filename + ".rtf")
                    {
                        dic_treeView.SelectedNode = current_node.Nodes[i];
                        dic_treeView.Focus();
                        string filepath = getnodepath(current_node.Nodes[i]);
                        filename_textBox.Text = filename;
                        note_richTextBox.LoadFile(filepath);
                        FileInfo fi = new FileInfo(filepath);
                        createdtime_label.Text = fi.CreationTime.ToString();
                        modifedtime_label.Text = fi.LastWriteTime.ToString();
                        current_node = current_node.Nodes[i];
                        break;
                    }
                }
            }
        }
        //颜色设置
        private void color_button_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            colorDialog.AllowFullOpen = true;
            colorDialog.FullOpen = true;
            colorDialog.ShowHelp = true;
            colorDialog.ShowDialog();
            //MessageBox.Show(colorDialog.Color.ToString());
            note_richTextBox.SelectionColor = colorDialog.Color;
        }

        private void red_button_Click(object sender, EventArgs e)
        {
            note_richTextBox.SelectionColor = Color.Red;
        }

        private void yellow_button_Click(object sender, EventArgs e)
        {
            note_richTextBox.SelectionColor = Color.Yellow;
        }

        private void black_button_Click(object sender, EventArgs e)
        {
            note_richTextBox.SelectionColor = Color.Black;
        }
        //查找
        private void search_button_Click(object sender, EventArgs e)
        {
            if((current_folder==null)||current_folder.Text.EndsWith(".rtf"))
            {
                MessageBox.Show("请选择一个文件夹后在查找文件!");
                return;
            }
            string find_filename = find_textBox.Text;
            string show = "";
            for (int i = 0; i < current_folder.Nodes.Count; i++)
            { 
               if(find_filename==current_folder.Nodes[i].Text.Substring(0,current_folder.Nodes[i].Text.Length-4))
               {
                        string filepath=getnodepath(current_folder.Nodes[i]);
                        RichTextBox temp_rtb=new RichTextBox();
                        temp_rtb.LoadFile(filepath);
                        string filecontent = temp_rtb.Text;
                        FileInfo fi = new FileInfo(filepath);
                        string createdtime= fi.CreationTime.ToString();
                        string modifedtime= fi.LastWriteTime.ToString();
                        show = find_filename +"  "+"创建时间:"+createdtime+"  "+"修改时间:" +modifedtime+"\n" + filecontent;
                        file_listBox.SelectedItem = show;
               }
                   
            }
            if (show == "")
                MessageBox.Show("当前文件夹下无此文件！");
        }
        //菜单新建文件夹
        private void 新建文件夹ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < current_folder.Nodes.Count; i++)
            {
                if (current_folder.Nodes[i].Text == "新建文件夹")
                {
                    MessageBox.Show("已有新建文件夹,请修改原文件夹名!");
                    return;
                }
            }
            string nodepath = getnodepath(current_folder) + "\\新建文件夹";
            TreeNode newnode = new TreeNode("新建文件夹");
            current_folder.Nodes.Add(newnode);
            System.IO.Directory.CreateDirectory(nodepath);
        }
        //菜单新建文件
        private void 新建文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (current_node.Text.EndsWith(".rtf"))
                current_node = current_node.Parent;
            for (int i = 0; i < current_node.Nodes.Count; i++)
            {
                if (current_node.Nodes[i].Text == "新建文件.rtf")
                {
                    MessageBox.Show("已有新建文件,请修改原文件名!");
                    return;
                }
            }
            string nodepath = getnodepath(current_node) + "\\新建文件.rtf";
            TreeNode newnode = new TreeNode("新建文件.rtf");
            current_node.Nodes.Add(newnode);
            current_node = newnode;
            dic_treeView.SelectedNode = newnode;
            System.IO.File.CreateText(nodepath);
            filename_textBox.Enabled = true;
            note_richTextBox.Enabled = true;
            edit_toolStrip.Enabled = true;
            note_richTextBox.Clear();
            createdtime_label.Text = "";
            modifedtime_label.Text = "";
            filename_textBox.Text = "新建文件";
        }
        private void 同步ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (current_node.Text != (filename_textBox.Text + ".rtf"))
            {
                current_node.Text = filename_textBox.Text + ".rtf";
                string newfilepath = getnodepath(current_node);
                System.IO.File.Move(sourcepath, newfilepath);
                FileInfo fi = new FileInfo(newfilepath);
                createdtime_label.Text = fi.CreationTime.ToString();
                modifedtime_label.Text = fi.LastWriteTime.ToString();
            }
            else
            {
                string newfilepath = getnodepath(current_node);
                this.note_richTextBox.SaveFile(@newfilepath);
                FileInfo fi = new FileInfo(newfilepath);
                createdtime_label.Text = fi.CreationTime.ToString();
                modifedtime_label.Text = fi.LastWriteTime.ToString();
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 海底世界ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            海底世界ToolStripMenuItem.Checked = true;
            紫色诱惑ToolStripMenuItem.Checked = false;
            红色幻想ToolStripMenuItem.Checked = false;
            千与千寻ToolStripMenuItem.Checked = false;
            淡雅绿叶ToolStripMenuItem.Checked = false;
            朦胧之心ToolStripMenuItem.Checked = false;
            this.BackgroundImage = Image.FromFile("海底世界.png");

        }

        private void 紫色诱惑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            海底世界ToolStripMenuItem.Checked = false;
            紫色诱惑ToolStripMenuItem.Checked = true;
            红色幻想ToolStripMenuItem.Checked = false;
            千与千寻ToolStripMenuItem.Checked = false;
            淡雅绿叶ToolStripMenuItem.Checked = false;
            朦胧之心ToolStripMenuItem.Checked = false;
            this.BackgroundImage = Image.FromFile("紫色诱惑.png");
        }

        private void 红色幻想ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            海底世界ToolStripMenuItem.Checked = false;
            紫色诱惑ToolStripMenuItem.Checked = false;
            红色幻想ToolStripMenuItem.Checked = true;
            千与千寻ToolStripMenuItem.Checked = false;
            淡雅绿叶ToolStripMenuItem.Checked = false;
            朦胧之心ToolStripMenuItem.Checked = false;
            this.BackgroundImage = Image.FromFile("红色幻想.png");
        }

        private void 千与千寻ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            海底世界ToolStripMenuItem.Checked = false;
            紫色诱惑ToolStripMenuItem.Checked = false;
            红色幻想ToolStripMenuItem.Checked = false;
            千与千寻ToolStripMenuItem.Checked = true;
            淡雅绿叶ToolStripMenuItem.Checked = false;
            朦胧之心ToolStripMenuItem.Checked = false;
            this.BackgroundImage = Image.FromFile("千与千寻.png");

        }

        private void 淡雅绿叶ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            海底世界ToolStripMenuItem.Checked = false;
            紫色诱惑ToolStripMenuItem.Checked = false;
            红色幻想ToolStripMenuItem.Checked = false;
            千与千寻ToolStripMenuItem.Checked = false;
            淡雅绿叶ToolStripMenuItem.Checked = true;
            朦胧之心ToolStripMenuItem.Checked = false;
            this.BackgroundImage = Image.FromFile("清新绿叶.png");

        }

        private void 朦胧之心ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            海底世界ToolStripMenuItem.Checked = false;
            紫色诱惑ToolStripMenuItem.Checked = false;
            红色幻想ToolStripMenuItem.Checked = false;
            千与千寻ToolStripMenuItem.Checked = false;
            淡雅绿叶ToolStripMenuItem.Checked = false;
            朦胧之心ToolStripMenuItem.Checked = true;
            this.BackgroundImage = Image.FromFile("朦胧爱心.png");
        }


       
       
    }
    class FileInfor
    {
        private
        string filename;
        string createtime;
        string modifetime;
        string filepath;
        public FileInfor(string fn, string ct, string mt, string fp)
        {
            filename = fn;
            createtime = ct;
            modifetime = mt;
            filepath = fp;
        }

        public string Filename
        {
            get { return filename; }
            set { filename = value; }
        }
        public string Createtime
        {
            get { return createtime; }
            set { createtime = value; }
        }
        public string Modifetime
        {
            get { return modifetime; }
            set { modifetime = value; }
        }
        public string Filepath
        {
            get { return filepath; }
            set { filepath = value; }
        }

    };
}
